home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 198_01 / basic.c < prev    next >
C/C++ Source or Header  |  1990-01-21  |  17KB  |  626 lines

  1. /*
  2.  * The routines in this file move the cursor around on the screen. They
  3.  * compute a new value for the cursor, then adjust ".". The display code
  4.  * always updates the cursor location, so only moves between lines, or
  5.  * functions that adjust the top line in the window and invalidate the
  6.  * framing, are hard.
  7.  */
  8. #include        <stdio.h>
  9. #include    "estruct.h"
  10. #include        "edef.h"
  11.  
  12. /*
  13.  * Move the cursor to the
  14.  * beginning of the current line.
  15.  * Trivial.
  16.  */
  17. gobol(f, n)
  18. {
  19.         curwp->w_doto  = 0;
  20.         return (TRUE);
  21. }
  22.  
  23. /*
  24.  * Move the cursor backwards by "n" characters. If "n" is less than zero call
  25.  * "forwchar" to actually do the move. Otherwise compute the new cursor
  26.  * location. Error if you try and move out of the buffer. Set the flag if the
  27.  * line pointer for dot changes.
  28.  */
  29. backchar(f, n)
  30. register int    n;
  31. {
  32.         register LINE   *lp;
  33.  
  34.         if (n < 0)
  35.                 return (forwchar(f, -n));
  36.         while (n--) {
  37.                 if (curwp->w_doto == 0) {
  38.                         if ((lp=lback(curwp->w_dotp)) == curbp->b_linep)
  39.                                 return (FALSE);
  40.                         curwp->w_dotp  = lp;
  41.                         curwp->w_doto  = llength(lp);
  42.                         curwp->w_flag |= WFMOVE;
  43.                 } else
  44.                         curwp->w_doto--;
  45.         }
  46.         return (TRUE);
  47. }
  48.  
  49. /*
  50.  * Move the cursor to the end of the current line. Trivial. No errors.
  51.  */
  52. goeol(f, n)
  53. {
  54.         curwp->w_doto  = llength(curwp->w_dotp);
  55.         return (TRUE);
  56. }
  57.  
  58. /*
  59.  * Move the cursor forwards by "n" characters. If "n" is less than zero call
  60.  * "backchar" to actually do the move. Otherwise compute the new cursor
  61.  * location, and move ".". Error if you try and move off the end of the
  62.  * buffer. Set the flag if the line pointer for dot changes.
  63.  */
  64. forwchar(f, n)
  65. register int    n;
  66. {
  67.         if (n < 0)
  68.                 return (backchar(f, -n));
  69.         while (n--) {
  70.                 if (curwp->w_doto == llength(curwp->w_dotp)) {
  71.                         if (curwp->w_dotp == curbp->b_linep)
  72.                                 return (FALSE);
  73.                         curwp->w_dotp  = lforw(curwp->w_dotp);
  74.                         curwp->w_doto  = 0;
  75.                         curwp->w_flag |= WFMOVE;
  76.                 } else
  77.                         curwp->w_doto++;
  78.         }
  79.         return (TRUE);
  80. }
  81.  
  82. gotoline(f, n)        /* move to a particular line.
  83.                argument (n) must be a positive integer for
  84.                this to actually do anything        */
  85.  
  86. {
  87.     register int status;    /* status return */
  88.     char arg[NSTRING];    /* buffer to hold argument */
  89.  
  90.     /* get an argument if one doesnt exist */
  91.     if (f == FALSE) {
  92.         if ((status=mlreply("Line to GOTO: ", arg, NSTRING)) != TRUE) {
  93.             mlwrite("[Aborted]");
  94.             return(status);
  95.         }
  96.         n = atoi(arg);
  97.     }
  98.  
  99.     if (n < 1)        /* if a bogus argument...then leave */
  100.         return(FALSE);
  101.  
  102.     /* first, we go to the start of the buffer */
  103.         curwp->w_dotp  = lforw(curbp->b_linep);
  104.         curwp->w_doto  = 0;
  105.     return(forwline(f, n-1));
  106. }
  107.  
  108. /*
  109.  * Goto the beginning of the buffer. Massive adjustment of dot. This is
  110.  * considered to be hard motion; it really isn't if the original value of dot
  111.  * is the same as the new value of dot. Normally bound to "M-<".
  112.  */
  113. gobob(f, n)
  114. {
  115.         curwp->w_dotp  = lforw(curbp->b_linep);
  116.         curwp->w_doto  = 0;
  117.         curwp->w_flag |= WFHARD;
  118.         return (TRUE);
  119. }
  120.  
  121. /*
  122.  * Move to the end of the buffer. Dot is always put at the end of the file
  123.  * (ZJ). The standard screen code does most of the hard parts of update.
  124.  * Bound to "M->".
  125.  */
  126. goeob(f, n)
  127. {
  128.         curwp->w_dotp  = curbp->b_linep;
  129.         curwp->w_doto  = 0;
  130.         curwp->w_flag |= WFHARD;
  131.         return (TRUE);
  132. }
  133.  
  134. /*
  135.  * Move forward by full lines. If the number of lines to move is less than
  136.  * zero, call the backward line function to actually do it. The last command
  137.  * controls how the goal column is set. Bound to "C-N". No errors are
  138.  * possible.
  139.  */
  140. forwline(f, n)
  141. {
  142.         register LINE   *dlp;
  143.  
  144.         if (n < 0)
  145.                 return (backline(f, -n));
  146.  
  147.     /* flag this command as a line move */
  148.         thisflag |= CFCPCN;
  149.  
  150.     /* if we are on the last line as we start....fail the command */
  151.     if (curwp->w_dotp == curbp->b_linep)
  152.         return(FALSE);
  153.  
  154.     /* if the last command was not note a line move,
  155.        reset the goal column */
  156.         if ((lastflag&CFCPCN) == 0)
  157.                 curgoal = getccol(FALSE);
  158.  
  159.     /* and move the point down */
  160.         dlp = curwp->w_dotp;
  161.         while (n-- && dlp!=curbp->b_linep)
  162.                 dlp = lforw(dlp);
  163.  
  164.     /* reseting the current position */
  165.         curwp->w_dotp  = dlp;
  166.         curwp->w_doto  = getgoal(dlp);
  167.         curwp->w_flag |= WFMOVE;
  168.         return (TRUE);
  169. }
  170.  
  171. /*
  172.  * This function is like "forwline", but goes backwards. The scheme is exactly
  173.  * the same. Check for arguments that are less than zero and call your
  174.  * alternate. Figure out the new line and call "movedot" to perform the
  175.  * motion. No errors are possible. Bound to "C-P".
  176.  */
  177. backline(f, n)
  178. {
  179.         register LINE   *dlp;
  180.  
  181.         if (n < 0)
  182.                 return (forwline(f, -n));
  183.  
  184.  
  185.     /* flag this command as a line move */
  186.         thisflag |= CFCPCN;
  187.  
  188.     /* if we are on the last line as we start....fail the command */
  189.     if (lback(curwp->w_dotp) == curbp->b_linep)
  190.         return(FALSE);
  191.  
  192.     /* if the last command was not note a line move,
  193.        reset the goal column */
  194.         if ((lastflag&CFCPCN) == 0)
  195.                 curgoal = getccol(FALSE);
  196.  
  197.     /* and move the point up */
  198.         dlp = curwp->w_dotp;
  199.         while (n-- && lback(dlp)!=curbp->b_linep)
  200.                 dlp = lback(dlp);
  201.  
  202.     /* reseting the current position */
  203.         curwp->w_dotp  = dlp;
  204.         curwp->w_doto  = getgoal(dlp);
  205.         curwp->w_flag |= WFMOVE;
  206.         return (TRUE);
  207. }
  208.  
  209. #if    WORDPRO
  210. gobop(f, n)    /* go back to the beginning of the current paragraph
  211.            here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
  212.            combination to delimit the beginning of a paragraph    */
  213.  
  214. int f, n;    /* default Flag & Numeric argument */
  215.  
  216. {
  217.     register int suc;    /* success of last backchar */
  218.  
  219.     if (n < 0)    /* the other way...*/
  220.         return(goeop(f, -n));
  221.  
  222.     while (n-- > 0) {    /* for each one asked for */
  223.  
  224.         /* first scan back until we are in a word */
  225.         suc = backchar(FALSE, 1);
  226.         while (!inword() && suc)
  227.             suc = backchar(FALSE, 1);
  228.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  229.  
  230.         /* and scan back until we hit a <NL><NL> or <NL><TAB>
  231.            or a <NL><SPACE>                    */
  232.         while (lback(curwp->w_dotp) != curbp->b_linep)
  233.             if (llength(curwp->w_dotp) != 0 &&
  234.                 lgetc(curwp->w_dotp, curwp->w_doto) != TAB &&
  235.                 lgetc(curwp->w_dotp, curwp->w_doto) != ' ')
  236.                 curwp->w_dotp = lback(curwp->w_dotp);
  237.             else
  238.                 break;
  239.  
  240.         /* and then forward until we are in a word */
  241.         if (lback(curwp->w_dotp) == curbp->b_linep)
  242.             suc = (curwp->w_dotp != curbp->b_linep);
  243.         else
  244.             suc = forwchar(FALSE, 1);
  245.         while (suc && !inword())
  246.             suc = forwchar(FALSE, 1);
  247.     }
  248.     curwp->w_flag |= WFMOVE;    /* force screen update */
  249.     return(TRUE);
  250. }
  251.  
  252. goeop(f, n)    /* go forword to the end of the current paragraph
  253.            here we look for a <NL><NL> or <NL><TAB> or <NL><SPACE>
  254.            combination to delimit the beginning of a paragraph    */
  255.  
  256. int f, n;    /* default Flag & Numeric argument */
  257.  
  258. {
  259.     register int suc;    /* success of last backchar */
  260.  
  261.     if (n < 0)    /* the other way...*/
  262.         return(gobop(f, -n));
  263.  
  264.     while (n-- > 0) {    /* for each one asked for */
  265.  
  266.         /* first scan forward until we are in a word */
  267.         suc = TRUE; /* forwchar(FALSE, 1); */
  268.         while (!inword() && suc)
  269.             suc = forwchar(FALSE, 1);
  270.         curwp->w_doto = 0;    /* and go to the B-O-Line */
  271.         if (suc)    /* of next line if not at EOF */
  272.             curwp->w_dotp = lforw(curwp->w_dotp);
  273.  
  274.         /* and scan forword until we hit a <NL><NL> or <NL><TAB>
  275.            or a <NL><SPACE>                    */
  276.         while (curwp->w_dotp != curbp->b_linep) {
  277.             if (llength(curwp->w_dotp) != 0 &&
  278.                 lgetc(curwp->w_dotp, curwp->w_doto) != TAB &&
  279.                 lgetc(curwp-